﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCrawlerLib;
using HtmlAgilityPack;

namespace WCrawler
{
    public partial class frmTopics : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dropDownListParentThread.Items.Insert(0, new ListItem());
                dropDownListParentThread.DataBind();
                dropDownListParentThread.SelectedIndex = 0;
            }
        }

        protected void chkViewEdit_CheckedChanged(object sender, EventArgs e)
        {
            lblUpdateMessage.Text = "";
            gridViewThread.DataBind();
        }


        protected void ucTreeViewTopic_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ucTreeViewTopic.SelectedNode != null)
            {
                Session["queryNodes"] = ucTreeViewTopic.GetTopics(ucTreeViewTopic.SelectedNode);
                gridViewThread.DataBind();
            }
        }

        protected void btnSubmitChanges_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gridViewThread.Rows)
            {
                //Get controls
                TextBox txtTopicName = (TextBox)row.FindControl("txtTopicName");
                DropDownList dropDownListParentTopic = (DropDownList)row.FindControl("ParentTopicDropDownList");
                TextBox txtTopicOrder = (TextBox)row.FindControl("txtTopicOrder");
                TextBox txtKeywords = (TextBox)row.FindControl("txtKeywords");

                int topicId = (int)gridViewThread.DataKeys[row.RowIndex].Values["TopicId"];
                //Get ForumTopic object by forumid and threadid
                Topic objTopic = new Topic();
                objTopic = objTopic.GetTopic(topicId);
                objTopic.TopicName = txtTopicName.Text;
                if (!string.IsNullOrEmpty(txtTopicOrder.Text))
                    objTopic.TopicOrder = int.Parse(txtTopicOrder.Text);
                objTopic.Keywords = txtKeywords.Text;
                if (!string.IsNullOrEmpty(dropDownListParentTopic.SelectedValue))
                    objTopic.ParentTopicId = int.Parse(dropDownListParentTopic.SelectedValue);
                objTopic.UpdateTopic(objTopic);
            }
            gridViewThread.DataBind();
            lblUpdateMessage.Text = "Update successful!";
        }

        protected void gridViewForumThreadDetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Init parentthread
                int topicId = (int)gridViewThread.DataKeys[e.Row.RowIndex].Values["TopicId"];
                //Get ForumTopic object by forumid and threadid
                Topic objTopic = new Topic();
                objTopic = objTopic.GetTopic(topicId);
                DropDownList parentTopicDropDownList = (DropDownList)e.Row.FindControl("ParentTopicDropDownList");
                parentTopicDropDownList.Items.Insert(0, new ListItem());
                parentTopicDropDownList.DataBind();
                //Show selected parent topic in dropdownlist
                if (objTopic.ParentTopicId.HasValue)
                    parentTopicDropDownList.SelectedValue = objTopic.ParentTopicId.Value.ToString();
                else
                    parentTopicDropDownList.SelectedIndex = 0;

                //Show topic name with hierarchical
                Label lblTopicName = (Label)e.Row.FindControl("lblTopicName");
                if (gridViewThread.DataKeys[e.Row.RowIndex].Value.ToString() == ucTreeViewTopic.SelectedNode.Value
                    || ucTreeViewTopic.SelectedNode.Value == "0")
                {
                    lblTopicName.Text = lblTopicName.Text;
                }
                else
                {
                    lblTopicName.Text = " ......  " + lblTopicName.Text;
                }
            }
        }

        protected void btnAddThread_Click(object sender, EventArgs e)
        {
            Topic objNewTopic = new Topic();
            objNewTopic.TopicName = txtName.Text;
            if (!string.IsNullOrEmpty(dropDownListParentThread.SelectedValue))
                objNewTopic.ParentTopicId = int.Parse(dropDownListParentThread.SelectedValue);
            int id = objNewTopic.AddTopic(objNewTopic);
            if (id > 0)
                lblAddMessage.Text = "Add successful";
            else
                lblAddMessage.Text = "Name is exists";
            gridViewThread.DataBind();
            dropDownListParentThread.DataBind();
        }

        protected void TopicGridViewLinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            if (Session["queryNodes"] != null)
            {
                Topic obj = new Topic();
                e.Result = obj.GetTopicsByQuery(Session["queryNodes"].ToString());
                //Session["queryNodes"] = null;
            }
            else
            {
                Topic obj = new Topic();
                e.Result = obj.GetAllTopics();
            }
        }
    }
}